package com.zho.common.fileserver;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;
import java.util.UUID;

import com.zho.common.fileserver.FileServerHelper.FileType;
import com.zho.common.utility.Constants.DATE;
import com.zho.util.DateHelpers;
import com.zho.util.FilePathUtil;

public class FileServerBean {
    private List<String> listTypes = new ArrayList<String>();
    private String folder;

    /**
     * 
     * @param fileName
     * @return
     */
    public String getFileExt(String fileName) {
        if (fileName != null) {
            int i = fileName.lastIndexOf(".");
            if (i > -1) {
                return fileName.substring(i + 1);
            }
        }
        return null;
    }

    /**
     * 
     * @param fileName
     * @return
     */
    public String toLowCaseFileExt(String fileName) {
        if (fileName != null) {
            int i = fileName.lastIndexOf(".");
            if (i > -1) {
                return (fileName.substring(0, i + 1) + fileName.substring(i + 1).toLowerCase());
            }
        }
        return fileName;
    }
    
    /**
     * @return the folder
     */
    public String getFolder() {
        return folder;
    }

    /**
     * @param folder
     *            the folder to set
     */
    public void setFolder(String folder) {
        this.folder = folder;
    }

    /**
     * 
     * @param types
     */
    public void setTypes(String types) {
        if (types != null) {
            String type;
            StringTokenizer strToken = new StringTokenizer(";" + types + ";", ",;");
            while (strToken.hasMoreElements()) {
                type = strToken.nextToken();
                listTypes.add(type.trim().toLowerCase());
            }
            Collections.sort(listTypes);
        }
    }

    /**
     * 
     * @param type
     * @return
     */
    public boolean checkType(String type) {
        boolean rs = false;
        if (type != null) {
            return listTypes.contains(type.trim().toLowerCase());
        }
        return rs;
    }

    /**
     * 
     * @param uri
     * @return {folder}/uri
     */
    public String getFilePath(String uri) {
        return FilePathUtil.catString(folder, uri);
    }

    /**
     * 
     * @param type
     * @return {folder}/[YYYY/MM/DD]/[UUID].[type]
     */
    public String generateFilePath(FileType type) {
        return getFilePath(generateFileUri(type));
    }

    /**
     * 
     * @param type
     * @return
     */
    public String generateFileUri(FileType type) {
        return generateFileUri(type.type());
    }

    /**
     * 
     * @param type
     * @param date
     * @return
     */
    public String generateFileUri(FileType type, Date date) {
        return generateFileUri(type.type(), date);
    }

    /**
     * 
     * @param type
     * @return
     */
    public String generateFileName(FileType type) {
        return generateFileName(type.type());
    }

    /**
     * 
     * @param type
     * @return
     */
    public String generateFileUri(String type) {
        return generateFileUri(type, new Date());
    }

    /**
     * 
     * @param type
     * @param date
     * @return
     */
    public String generateFileUri(String type, Date date) {
        String strDate = "";
        try {
            strDate = DateHelpers.dateToString(date, DATE.DATE_FORMAT_yyyMMdd);
        } catch (Exception e) {
        }
        return FilePathUtil.catString(strDate, generateFileName(type));
    }
    
    /**
     * 
     * @param type
     * @return
     */
    public String generateFileName(String type) {
        return UUID.randomUUID().toString() + "." + type;
    }

    /**
     * 
     */
    public String toString() {
        return "FileServerBean-[folder:" + folder + ", listTypes:" + listTypes + "]";
    }

}
